﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WHC.Hotel.Entity;
using WHC.Hotel.BLL;
using WHC.OrderWater.Commons;


namespace WHC.Hotel.UI
{
    public partial class FrmSuiteDiscount : BaseForm
    {
        public string ID = string.Empty;
        private bool dirty = false;

        public FrmSuiteDiscount()
        {
            InitializeComponent();
        }

        private void FrmRoomDiscount_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(ID))
            {
                TeaSuiteTypeInfo typeInfo = BLLFactory<TeaSuiteType>.Instance.FindByID(ID);
                if (typeInfo != null)
                {
                    this.lblItemName.Text = typeInfo.Name;
                    this.lblPrice.Text = typeInfo.Price.ToString("f2");

                    this.dg.AutoGenerateColumns = false;
                    this.dg.DataSource = BLLFactory<TeaSuiteType>.Instance.GetDiscountList(typeInfo.Name);
                    this.dg.Columns[0].DataPropertyName = "CustomerGrade";
                    this.dg.Columns[1].DataPropertyName = "DiscountRate";
                    this.dg.Columns[2].DataPropertyName = "DiscountPrice";
                }
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(ID) && dirty)
            {
                TeaSuiteTypeInfo typeInfo = BLLFactory<TeaSuiteType>.Instance.FindByID(ID);
                if (typeInfo != null)
                {
                    BLLFactory<TeaSuiteDiscount>.Instance.DeleteAll(typeInfo.Name);
                    foreach (DataGridViewRow row in this.dg.Rows)
                    {
                        TeaSuiteDiscountInfo info = new TeaSuiteDiscountInfo();
                        info.Price = typeInfo.Price;
                        info.RoomType = typeInfo.Name;

                        info.CustomerGrade = row.Cells[0].Value.ToString();
                        info.DiscountRate = Convert.ToDouble(row.Cells[1].Value.ToString());
                        info.DiscountPrice = Convert.ToDecimal(row.Cells[2].Value);

                        BLLFactory<TeaSuiteDiscount>.Instance.Insert(info);
                    }

                    dirty = false;
                    MessageUtil.ShowTips("保存成功");
                }
            }
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            if (dirty)
            {
                if (MessageUtil.ShowYesNoAndWarning("您已经对数据进行修改，是否放弃数据保存？") == DialogResult.No)
                {
                    this.DialogResult = DialogResult.None;
                    return;
                }
            }

            this.Close();
        }

        private void dg_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            foreach (DataGridViewRow row in this.dg.SelectedRows)
            {
                if (!string.IsNullOrEmpty(ID))
                {
                    TeaSuiteTypeInfo typeInfo = BLLFactory<TeaSuiteType>.Instance.FindByID(ID);
                    if (typeInfo != null)
                    {
                        FrmEditDiscount dlg = new FrmEditDiscount();
                        dlg.lblCustomerGrade.Text = typeInfo.Name;
                        dlg.txtPrice.Text = typeInfo.Price.ToString("f2");

                        dlg.txtDiscountRate.Text = row.Cells[1].Value.ToString();
                        dlg.txtDiscountPrice.Text = row.Cells[2].Value.ToString();

                        if (dlg.ShowDialog() == DialogResult.OK)
                        {
                            //Save
                            row.Cells[1].Value = Convert.ToDecimal(dlg.txtDiscountRate.Text);
                            row.Cells[2].Value = Convert.ToDecimal(dlg.txtDiscountPrice.Text);

                            dirty = true;
                        }
                    }
                }
                break;
            }
        }
    }
}
